//
//  HMStatusToolBar.swift
//  WeiBo
//
//  Created by Apple on 16/9/21.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
//  微博toolBar
class HMStatusToolBar: UIView {

    private lazy var retweetButton: UIButton = self.addChildButton(title: "转发", imageName: "timeline_icon_retweet")
    private lazy var commentButton: UIButton = self.addChildButton(title: "评论", imageName: "timeline_icon_comment")
    private lazy var unlikeButton: UIButton = self.addChildButton(title: "赞", imageName: "timeline_icon_unlike")
    
    var statusViewModel: HMStatusViewModel? {
        didSet {
            retweetButton.setTitle(statusViewModel?.retweetCountContent, for: .normal)
            commentButton.setTitle(statusViewModel?.commentCountContent, for: .normal)
            unlikeButton.setTitle(statusViewModel?.unLinkCountContent, for: .normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
       
        
        //  设置约束
        retweetButton.snp_makeConstraints { (make) in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        commentButton.snp_makeConstraints { (make) in
            make.leading.equalTo(retweetButton.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton)
        }
        unlikeButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(self)
            make.bottom.equalTo(self)
            make.top.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
        }
        
        //  添加线
        let firstLine = addChildLineView()
        let secondLine = addChildLineView()
        firstLine.snp_makeConstraints { (make) in
            make.centerX.equalTo(retweetButton.snp_trailing)
            make.centerY.equalTo(self)
        }
        secondLine.snp_makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton.snp_trailing)
        }
        
        
    }
    
    //  添加子控件
    private func addChildButton(title: String, imageName: String) -> UIButton {
        let button = UIButton()
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        //  去掉高亮
        button.adjustsImageWhenHighlighted = false
        //  添加控件
        addSubview(button)
    
        return button
    }
    
    //  添加竖线
    private func addChildLineView() -> UIImageView {
    
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
    

}
